--[[
@author Aurelien Deharbe

This file is part of Cute-Client.

Cute-Client is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Cute-Client is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Cute-Client.  If not, see <http://www.gnu.org/licenses/>.
]]

module("cuteclient", package.seeall)

local socket = require("socket")
local cssession = require("cssession")

CSClient = {}
CSClient.__index = CSClient

function CSClient.init(host, port)
	local csclient = {}
	setmetatable(csclient, CSClient)
	
	local err
	csclient.serversocket, err = socket.connect(host, port)
	return csclient
end

function CSClient:newSession(sessid)
	return self:accessSession("QSSessionNew("..sessid..")\n")
end

function CSClient:joinSession(sessid)
	return self:accessSession("QSSessionJoin("..sessid..")\n")
end

function CSClient:accessSession(cmd)
	local session, sessport
	self.serversocket:send(cmd)
	self.serversocket:receive()
	session = cssession.CSSession.init()
	sessport = session:getPort()
	self.serversocket:send("QSSessionPort("..sessport..")\n")
	self.serversocket:receive()
	session:accept()
	return session
end
